package com.wjm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wjm.entity.Classroom;
import com.wjm.entity.Desk;
import com.wjm.mapper.interf.ClassroomMapper;
import com.wjm.service.interf.ClassroomServiceInterface;
import com.wjm.util.CacheUtil;

@Service("classroomService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ClassroomServiceImpl implements ClassroomServiceInterface {

	@Autowired
	private ClassroomMapper classroomMapper;

	@Override
	public boolean save(Classroom object) throws Exception {
		boolean result = false;
		if (classroomMapper.save(object) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean delete(int objectId) throws Exception {
		boolean result = false;
		if (classroomMapper.delete(objectId) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean update(Classroom object) throws Exception {
		boolean result = false;
		if (classroomMapper.update(object) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public int saveBatch(List<Classroom> list) throws Exception {
		return classroomMapper.saveBatch(list);
	}

	@Override
	public List<Classroom> queryByPage(int current, int number, Classroom cr)
			throws Exception {
		current = number * (current - 1);
		return classroomMapper.selectByPage(current, number, cr);
	}

	@Override
	public int queryTotal(int buId, int status) throws Exception {
		return classroomMapper.selectTotal(buId, status);
	}

	@Override
	public List<Classroom> queryByBu(int buId) throws Exception {
		return classroomMapper.selectByBu(buId);
	}

	@Override
	public int queryBelong(int crId) throws Exception {
		return classroomMapper.selectBelong(crId);
	}

	@Override
	public void checkUnuse(int crId, int buId) throws Exception {
		List<Desk> desks = CacheUtil.getDesks().get(crId);
		for (Desk desk : desks) {
			if (CacheUtil.getRecords().get(desk.getDeskId()) == null) {
				return;
			}
		}
		// int buId = classroomMapper.selectBelong(crId);
		List<Classroom> rooms = CacheUtil.getUseroom().get(buId);
		for (int i = 0; i < rooms.size(); i++) {
			Classroom room = rooms.get(i);
			// 找到该觉时候将其放入不可用队列
			if (room.getCrId() == crId) {
				List<Classroom> unuse = CacheUtil.getUnuseroom().get(buId);
				if (unuse == null) {
					unuse = new ArrayList<>();
					CacheUtil.getUnuseroom().put(buId, unuse);
				}
				unuse.add(room);
				rooms.remove(i);
				break;
			}
		}
	}

	@Override
	public void checkUse(int crId) throws Exception {
		int buId = classroomMapper.selectBelong(crId);
		List<Classroom> rooms = CacheUtil.getUnuseroom().get(buId);
		if (rooms != null) {
			for (int i = 0; i < rooms.size(); i++) {
				Classroom room = rooms.get(i);
				if (room.getCrId() == crId) {
					List<Classroom> use = CacheUtil.getUseroom().get(buId);
					if (use == null) {
						use = new ArrayList<>();
						CacheUtil.getUseroom().put(buId, use);
					}
					use.add(room);
					rooms.remove(i);
					break;
				}
			}
		}
	}

}
